package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.SetmealMapper;
import com.sky.service.WorkSpaceService;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.SetmealOverViewVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import java.util.HashMap;


@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
@Autowired
private SetmealMapper setmealMapper;

    @Override
    public BusinessDataVO businessData(LocalDateTime begin, LocalDateTime end) {
        //查询新增用户总数

        Integer totalOrderCount = 0;
        Integer validOrderCount = 0;

        HashMap hashMap = new HashMap();
       hashMap.put("begin",begin);
       hashMap.put("end",end);
        Integer integer = userMapper.conutMap(hashMap);

        //查询订单完成率
        // 查询订单总数
        Integer totalOrder = orderMapper.countByMap(hashMap);
        hashMap.put("status", Orders.COMPLETED);
        Integer validOrder = orderMapper.countByMap(hashMap);
            totalOrderCount+=totalOrder;
            validOrderCount+=validOrder;
        Double orderCompletionRate =0.0;
        if (totalOrderCount !=0){
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount.doubleValue();
        }
        //查询营业额

        Double turnover = orderMapper.sumByMap(hashMap);

        //查询平均客单价
        Double unitPrice = 0.0;
        if (validOrderCount !=0){
            unitPrice = turnover / validOrderCount;
        }


         //查询有效订单数

        Integer validOrder1 = orderMapper.countByMap(hashMap);


        //构建对象返回BusinessDataVO
        return BusinessDataVO.builder()
                .newUsers(integer)
                .turnover(turnover)
                .validOrderCount(validOrder1)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        HashMap hashMap = new HashMap();
        hashMap.put("satus", StatusConstant.ENABLE);
     Integer sold =   setmealMapper.countByMap(hashMap);
     hashMap.put("status",StatusConstant.DISABLE);
     Integer discontinued = setmealMapper.countByMap(hashMap);
     return SetmealOverViewVO.builder()
             .sold(sold)
             .discontinued(discontinued)
             .build();


    }
}
